package cn.stylefeng.guns.modular.util.oss;

import cn.stylefeng.guns.config.AliyunOSSConfig;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectResult;

import java.io.File;
import java.io.InputStream;

/**
 * @author by jzy on 2019/11/21.
 */
public class UploadFile2OSS {


    //// endpoint以杭州为例，其它region请按实际情况填写
    //private String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    ////private String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
    //// 泄露者死
    //private String accessKeyId = "LTAI4FjLHsRDoR58Bs5BBhaf";
    //private String accessKeySecret = "m4zYXSEgLNhZYOgIcVUAGg9ILwb0Py";
    private OSSClient ossClient;

    public UploadFile2OSS(AliyunOSSConfig aliyunOSSConfig) {
        ossClient = new OSSClient(aliyunOSSConfig.getEndpoint(), aliyunOSSConfig.getAccessKeyId(), aliyunOSSConfig.getAccessKeySecret());
    }

    //DEMO
//    public static void main(String[] args) {
//        UploadFile2OSS oss = new UploadFile2OSS();
//        //oss.uploadPublic("bobofly-workimage","logo/logo.png",new File("/data/logo/logo.png"));
//        oss.uploadPublic("tuotuo-dev", "bargain/rules.png", new File("/data/logo/rules.png"));
//        //oss.uploadPublic("bobofly-workimage","app/dev/ksh2.4.apk",new File("/data/logo/ksh2.4.apk"));
//        //oss.uploadPublic("bobofly-workimage","app/oracle-10g.rar",new File("/BaiduNetdiskDownload/oracle-10g.rar"));
//        oss.closeConnect();
//        //ObjectListing objectListing = up.listObjects("feibo-public-001","record/2018-02-02/feibo/001-002/");feibo/fang001
////        ObjectListing objectListing = oss.listObjects("feibo-public-001","record/feibo/1517650504055100004/");
////        List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
////        oss.closeConnect();
//    }

    public void uploadPublic(String bucketName, String fileName, File file) {
        ossClient.putObject(bucketName, fileName, file);
    }

    public void uploadPublic(String bucketName, String fileName, String filePath) {
        ossClient.putObject(bucketName, fileName, new File(filePath));
    }

    public PutObjectResult uploadPublic(String bucketName, String fileName, InputStream in) {
        return ossClient.putObject(bucketName, fileName, in);
    }

    public ObjectListing listObjects(String bucketName, String prefix) {
        return ossClient.listObjects(bucketName, prefix);
    }

    public void closeConnect() {
        ossClient.shutdown();
    }


}
